<template>
  <div class='xtx-goods-page'>
    <div class="container">
      <!-- 面包屑 -->
      <XtxBread v-if="detail">
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem :to="`/category/${detail.categories[1].id}`">{{detail.categories[1].name}}</XtxBreadItem>
        <XtxBreadItem :to="`/category/sub/${detail.categories[0].id}`">{{detail.categories[0].name}}</XtxBreadItem>
        <XtxBreadItem>{{detail.name}}</XtxBreadItem>
      </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info">
         <div class="media">
           <GoodsImage :images='detail.mainPictures' />
         </div>
         <div class="spec"></div>
      </div>
      <!-- 商品推荐 -->
      <GoodsRelevant />
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <div class="goods-tabs"></div>
          <!-- 注意事项 -->
          <div class="goods-warn"></div>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside"></div>
      </div>
    </div>
  </div>
</template>

<script>
import GoodsRelevant from './components/goods-relevant'

import { findGoods } from '@/api/product'
// import {useStore} from 'vuex'
import { useRoute } from 'vue-router'
import { ref, watch } from 'vue'
import GoodsImage from './components/goods-image.vue'

// 获取商品列表的方法检出
const useGoods = () => {
  const route = useRoute()
  const detail = ref(null)
  watch(() => route.params.id, (newVal) => {
    // 如果不是商品详情路径 返回 不走此监听
    if (route.fullPath !== '/product/' + newVal) return
    // 获取商品的详情数据
    findGoods(route.params.id).then(res => {
      detail.value = res.result
    })
  }, {
    immediate: true
  })
  return detail
}

export default {
  name: 'XtxGoodsPage',
  components: { GoodsRelevant, GoodsImage },
  setup () {
    const detail = useGoods()
    return { detail }
  }
}
</script>

<style scoped lang='less'>
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
